package com.persondemo.videoappliction.ui.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.SearchBean;
import com.persondemo.videoappliction.bean.VideoBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.ui.adapter.RecyclerAdapter;
import com.persondemo.videoappliction.ui.adapter.SearchAdpater;
import com.persondemo.videoappliction.ui.adapter.SearchWaiAdapter;
import com.persondemo.videoappliction.ui.base.BaseActivity;
import com.persondemo.videoappliction.ui.search.contract.SearchContract;
import com.persondemo.videoappliction.ui.search.presenter.SearchPresenter;
import com.persondemo.videoappliction.ui.video.VideoDetilsActivity;
import com.persondemo.videoappliction.utils.FullyGridLayoutManager;
import com.persondemo.videoappliction.utils.T;
import com.persondemo.videoappliction.widget.CustomLoadMoreView;
import com.persondemo.videoappliction.widget.SimpleMultiStateView;
import com.persondemo.videoappliction.widget.SpaceItemDecoration;
import com.persondemo.videoappliction.widget.searchview.ICallBack;
import com.persondemo.videoappliction.widget.searchview.SearchView;
import com.persondemo.videoappliction.widget.searchview.bCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by Administrator on 2018/3/20.
 */

public class SearchActivity extends BaseActivity<SearchPresenter> implements SearchContract.View {
    public static final String TAG = "SearchActivity";
    @BindView(R.id.search_view)
    SearchView searchView;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.wai_recycler)
    RecyclerView waiRecycler;
    List<SearchBean.DataBean.BenlistBean> benlistBeen;
    List<SearchBean.DataBean.WailistBean> wailistBeen;
    SearchAdpater searchAdpater;
    SearchWaiAdapter searchWaiAdapter;
    @Override
    public void onRetry() {
    }

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_search;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {

    }

    @Override
    public void initData() {
        // 4. 设置点击搜索按键后的操作（通过回调接口）
        // 参数 = 搜索框输入的内容
        searchView.setOnClickSearch(new ICallBack() {
            @Override
            public void SearchAciton(String string) {
                showLoadingDialog();
                mPresenter.getserachdata(string);
            }
        });
        // 5. 设置点击返回按键后的操作（通过回调接口）
        searchView.setOnClickBack(new bCallBack() {
            @Override
            public void BackAciton() {
                finish();
            }
        });
        benlistBeen = new ArrayList<>();
        wailistBeen = new ArrayList<>();
        searchView.setOnClickListener(new Oncliseter() {
            @Override
            public void onItemClickListener(View v, String title) {
                showLoadingDialog();
                mPresenter.getserachdata(title);
            }
        });
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(10));
        waiRecycler.addItemDecoration(new SpaceItemDecoration(10));
    }

    @Override
    public void loaddata(SearchBean searchBean) {
        if (searchBean==null){
            T.showShort(SearchActivity.this,"没有更多数据！");
            return;
        }
        if (searchBean.getStatus() == 1) {
            showSuccess();
            hideLoadingDialog();
        }
        if (searchBean.getData().getBenlist() != null && searchBean.getData().getBenlist().size() > 0) {
            benlistBeen.clear();
            mRecyclerView.setLayoutManager(new GridLayoutManager(SearchActivity.this, 1));
            searchAdpater = new SearchAdpater(benlistBeen, SearchActivity.this);
            benlistBeen = searchBean.getData().getBenlist();
            searchAdpater.setNewData(benlistBeen);
            mRecyclerView.setAdapter(searchAdpater);
            searchAdpater.setEnableLoadMore(false);
            mRecyclerView.setNestedScrollingEnabled(false);
            searchAdpater.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            searchAdpater.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    gotoVideodetils(benlistBeen.get(position).getId());
                }
            });
        }
        if (searchBean.getData().getBenlist() != null && searchBean.getData().getWailist().size() > 0) {
            wailistBeen.clear();
            final FullyGridLayoutManager manager = new FullyGridLayoutManager(SearchActivity.this, 1);
            manager.setOrientation(GridLayoutManager.VERTICAL);
            manager.setSmoothScrollbarEnabled(true);
            waiRecycler.setLayoutManager(manager);
            searchWaiAdapter = new SearchWaiAdapter(wailistBeen, SearchActivity.this);
            wailistBeen = searchBean.getData().getWailist();
            searchWaiAdapter.setNewData(wailistBeen);
            waiRecycler.setAdapter(searchWaiAdapter);
            searchWaiAdapter.setEnableLoadMore(false);
            waiRecycler.setNestedScrollingEnabled(false);
            searchWaiAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            searchWaiAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    showLoadingDialog();
                    mPresenter.getwaidata(wailistBeen.get(position).getHref());
                }
            });
        } else {
            T.showShort(SearchActivity.this, searchBean.getMsg());
        }
    }

    @Override
    public void titleError() {
        T.showShort(SearchActivity.this, "搜索内容不能为空！");
    }

    @Override
    public void gotoVideodetils(int id) {
        hideLoadingDialog();
        Intent intent=new Intent(SearchActivity.this, VideoDetilsActivity.class);
        Bundle bundle=new Bundle();
        bundle.putInt("id",id);
        intent.putExtras(bundle);
        startActivity(intent);
    }

}
